/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.groupb.circus.Databases;

/**
 *
 * @author bhavna
 */
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class NewsHandler extends org.xml.sax.helpers.DefaultHandler {

    private News news;
    private StringBuffer accumulator;
    String id = "";

    public NewsHandler() {
        news = new News();
    }

    public NewsHandler(News news) {
        this.news = news;
    }

    // Called at the beginning
    @Override
    public void startDocument() {
        accumulator = new StringBuffer();
    }

    @Override
    public void characters(char[] buffer, int start, int length) {
        accumulator.setLength(0);
        accumulator.append(buffer, start, length);
    }

    /*
     *
     * At the beginning of each new element, erase any accumulated text
     *
     */
    @Override
    public void startElement(String namespaceURL, String localName, String qname, Attributes attributes) {

        accumulator.setLength(0);
        if (localName.equals("new")) {
            id = attributes.getValue("id");
        }
    }

    @Override
    public void endElement(String namespaceURL, String localName, String qname) {

        if (localName.equals("new")) {
            news.setId(id);

        }

        if (localName.equals("title")) {
            news.setTitle(accumulator.toString().trim());
        }


        if (localName.equals("day")) {
            news.setDay(accumulator.toString().trim());

        }

        if (localName.equals("month")) {
            news.setMonth(accumulator.toString().trim());
        }

        if (localName.equals("year")) {
            news.setYear(accumulator.toString().trim());
        }

        if (localName.equals("author")) {
            String author = (accumulator.toString().trim());
            news.setAuthor(author);
        }

        if (localName.equals("id_photo")) {
            
            news.setId_photo(accumulator.toString().trim());
        }

        if (localName.equals("paragraph")) {
            String paragraph = accumulator.toString().trim();
            news.setParagraph(paragraph);
        }
    }

    // Called at the end
    @Override
    public void endDocument() {
    }

    @Override
    public void warning(SAXParseException exception) {
        System.err.println("WARNING: line " + exception.getLineNumber() + ": "
                + exception.getMessage());
    }

    @Override
    public void error(SAXParseException exception) {
        System.err.println("ERROR: line " + exception.getLineNumber() + ": "
                + exception.getMessage());
    }

    @Override
    public void fatalError(SAXParseException exception) throws SAXException {
        System.err.println("FATAL: line " + exception.getLineNumber() + ": "
                + exception.getMessage());
        throw (exception);
    }
}